//引入
const express = require('express');
const db = require('../../utils/db')

//配置路由器
const level_HYH = express.Router();


//接口编写
//获取
level_HYH.get('/level/huoqu', (req, res) => {
    const page = req.query.page;
    const count = req.query.count;
    const sql = `select * from salary_level limit ${(page - 1) * count},${count}`;
    const sql2 = `select count(*) as total from salary_level;`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err })
        } else {
            db.query(sql2, (err2, data2) => {
                if (err2) {
                    console.log(err2);
                    res.send({ error: 1, msg: err2 });
                } else {
                    console.log(data2);
                    res.send({ error: 0, msg: '查询成功', data: data, total: data2[0].total });
                }
            })
        }
    })
})

// 删除
level_HYH.post('/level/shanchu', (req, res) => {
    const sql = 'delete from salary_level where p_id="' + req.body.id + '";';
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '删除成功' })
        }
    })
})

//查询搜索
level_HYH.post('/level/chaxun', (req, res) => {
    const level = req.body.level
    const title = req.body.title
    const sql = `SELECT * FROM salary_level WHERE l_title='${title}' OR l_level='${level}';`

    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err })
        } else {
            res.send({ error: 0, msg: '查询成功', data: data })
        }
    })
})

//修改
level_HYH.post('/level/xiugai', (req, res) => {
    const sql = `update salary_level set l_title='${req.body.title}',l_level='${req.body.level}',l_wage='${req.body.wage}' where l_id='${req.body.l_Id}';`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err });
        } else {
            res.send({ error: 0, msg: '修改成功', data: data })
        }
    })
})



//导出
module.exports = level_HYH;